package com.tjbaobao.videoplayol.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tjbaobao.videoplayer.view.base.AppBaseWidget;
import com.tjbaobao.videoplayol.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 图片选择组件
 * @author TJbaobao
 *
 */
public class AppImgChooseView extends AppBaseWidget implements OnClickListener {
	private int tvnNum =1;
	private int imgHeight ,imgWidth;
	private int viewWidth,viewHeight;
	private ArrayList<Bitmap> list = new ArrayList<Bitmap>();
	private Map<Integer,Boolean> numMap = new HashMap<Integer, Boolean>();
	private Map<Integer,RelativeLayout> rlMap = new HashMap<Integer, RelativeLayout>();
	private Map<Integer,View> tvMap = new HashMap<Integer, View>();
	private boolean isRun  = true ;
	Activity activity ;
	private int maxSize = 1;
	public AppImgChooseView(Context context, int imgWidth, int imgHeight)
	{
		super(context);
		this.activity = (Activity)context;
		this.imgWidth = imgWidth;
		this.imgHeight = imgHeight;
		cleanAll();
	}
	public void setRun(boolean isRun)
	{
		this.isRun = isRun ;
	}
	
	public View createView(Bitmap bitmap, int position)
	{
		int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
		viewWidth = (screenWidth - 4 * dipToPx(10)) / 3;
		viewHeight = viewWidth * imgHeight / imgWidth;
		
		RelativeLayout rl_index = new RelativeLayout(context);
		lParams = new LinearLayout.LayoutParams(viewWidth,viewHeight);
		lParams.setMargins(dipToPx(10), 0, 0, 0);
		rl_index.setLayoutParams(lParams);
		
		rl_index.addView(createImageView(bitmap, position));
		
		
		rlMap.put(position, rl_index);
		list.add(bitmap);
		return rl_index;
	}
	private ImageView createImageView(Bitmap bitmap, int position)
	{
		
		ImageView imageView = new ImageView(context);
		//rParams = new RelativeLayout.LayoutParams(R_MATCH_PARENT, R_MATCH_PARENT);
		rParams = new RelativeLayout.LayoutParams(viewWidth-dipToPx(4), viewHeight-dipToPx(4));
		rParams.setMargins(dipToPx(2), dipToPx(2), 0, 0);
		imageView.setLayoutParams(rParams);
		imageView.setImageBitmap(bitmap);
		imageView.setTag(position);
		numMap.put(position, false);
		imageView.setBackgroundResource(R.drawable.image_choose_view_bg);
		imageView.setOnClickListener(this);
		return imageView;
	}
	private View createNumView(int tvnNum)
	{
		TextView tv_num = new TextView(context);
		rParams = new RelativeLayout.LayoutParams(dipToPx(20),dipToPx(20));
		rParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		rParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		tv_num.setLayoutParams(rParams);
		tv_num.setGravity(Gravity.RIGHT| Gravity.BOTTOM);
		tv_num.setPadding(0, 0, dipToPx(1), -dipToPx(1));
		tv_num.setText(tvnNum+"");
		tv_num.setTextColor(Color.parseColor("#ffffff"));
		tv_num.setBackgroundResource(R.drawable.image_choose_view_on_icon);
		
		return tv_num;
	}
	/**
	 * 将dip数值转化为px数值
	 * @param dip
	 * @return
	 */
	private int dipToPx(int dip)
	{
		final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dip * scale + 0.5f);  
	}
	@Override
	public void onClick(View v) {
		if(!isRun)
		{
			return ;
		}
		int num = (Integer) v.getTag();
		RelativeLayout rl_index = rlMap.get(num);
		if(rl_index!=null)
		{
			//未选择
			if(!numMap.get(num))
			{
				if(tvnNum<=maxSize)
				{
					changeNumState(num);
					TextView view = (TextView) createNumView(tvnNum);
					rl_index.addView(view);
					rl_index.setTag(view.getText().toString());
					rl_index.setBackgroundResource(R.drawable.image_choose_view_press);
					tvMap.put(num, view);
					tvnNum++;
				}
			}
			else
			{
				changeNumState(num);
				rl_index.removeView(tvMap.get(num));
				rl_index.setBackgroundDrawable(null);
				TextView view = (TextView) tvMap.get(num);
				int nowNum = Integer.valueOf(view.getText().toString());
				for(int i=0;i<numMap.size();i++)
				{
					TextView view2 = (TextView) tvMap.get(i);
					if(view2!=null)
					{
						int nowNum2 = Integer.valueOf(view2.getText().toString());
						if(nowNum2>nowNum)
						{
							if(numMap.get(i))
							{
								view2.setText(nowNum2-1+"");
							}
						}
					}
				}
				tvnNum--;
			}
		}
	}
	private boolean changeNumState(int num)
	{
		if(!numMap.get(num))
		{
			numMap.put(num, true);
		}
		else
		{
			numMap.put(num, false);
		}
		return numMap.get(num);
	}
	public Bitmap getChooseBitmap(int num)
	{
		if(numMap.get(num))
		{
			return list.get(num);
		}
		return null;
	}
	public void cleanAll()
	{
		list.clear();
		numMap.clear();
		rlMap.clear();
		tvMap.clear();
	}
}
